"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Big = exports.BigEvolution = exports.BigState = void 0;
const Point_1 = require("./Point");
const GameUtils_1 = __importDefault(require("../GameUtils"));
/**
 * 大子状态
 */
var BigState;
(function (BigState) {
    BigState["HIDE"] = "\u6F5C\u884C";
    BigState["SHIELD"] = "\u62A4\u76FE";
    BigState["FEAR"] = "\u602F\u6218";
    BigState["ICE"] = "\u51B0\u51BB";
    BigState["BURDEN"] = "\u7126\u6E34";
    BigState["METHYSIS"] = "\u4E2D\u6BD2";
    BigState["METHYSIS_BEFORE"] = "\u5FAE\u6BD2";
    BigState["BLOODTHIRSTY"] = "\u55DC\u8840";
    BigState["WAVERIDER"] = "\u8E0F\u6D6A";
    BigState["FERTILE"] = "\u65FA\u80B2";
    BigState["EXPOSE"] = "\u66B4\u9732";
    BigState["HEROIC"] = "\u5E38\u80DC"; //常胜将军，攻击成功后获得获得护盾
})(BigState = exports.BigState || (exports.BigState = {}));
/**
 * 大子进化
 */
var BigEvolution;
(function (BigEvolution) {
    BigEvolution["\u672A\u8F6C\u578B"] = "\u672A\u8F6C\u578B";
    BigEvolution["\u5E7D\u7075"] = "\u5E7D\u7075";
    BigEvolution["\u6076\u7075"] = "\u6076\u7075";
    BigEvolution["\u70AE\u53F0"] = "\u70AE\u53F0";
    BigEvolution["\u72D9\u70AE"] = "\u72D9\u70AE";
    BigEvolution["\u987D\u77F3"] = "\u987D\u77F3";
    BigEvolution["\u5730\u7075"] = "\u5730\u7075";
    BigEvolution["\u7CBE\u7075"] = "\u7CBE\u7075";
    BigEvolution["\u6C99\u5175"] = "\u6C99\u5175";
    BigEvolution["\u6C99\u7687"] = "\u6C99\u7687";
    BigEvolution["\u91CE\u732A"] = "\u91CE\u732A";
    BigEvolution["\u91CE\u9A6C"] = "\u91CE\u9A6C";
    BigEvolution["\u62F3\u5E08"] = "\u62F3\u5E08";
    BigEvolution["\u7075\u9635"] = "\u7075\u9635";
    BigEvolution["\u5929\u8C15"] = "\u5929\u8C15";
    BigEvolution["\u5996\u672F"] = "\u5996\u672F";
    BigEvolution["\u9F13\u821E"] = "\u9F13\u821E";
    BigEvolution["\u6307\u6325"] = "\u6307\u6325";
})(BigEvolution = exports.BigEvolution || (exports.BigEvolution = {}));
/**
 * 大子
 */
class Big {
    constructor(evolution = BigEvolution.未转型) {
        this.arr_states = new Set();
        this.evolution = BigEvolution.未转型;
        this.actionTime = 0; //行动次数，所有棋子一回合只能行动2次，回合开始时重置
        this.is_death = false; //是否已经死亡，一般用不到
        this.evolution = evolution;
    }
    init(gameMgr, player, point, monther, father) {
        this.gameMgr = gameMgr;
        this.player = player;
        this.point = point;
        this.mother = monther;
        this.father = father;
        this.point.big = this;
        this.player.arr_bigs.add(this);
        this.id = player.username + "-" + (++player.num_bornBig).toString();
        this.terrainAdapt();
    }
    gc() {
        this.gameMgr = null;
        this.player = null;
        this.point = null;
        this.mother = null;
        this.father = null;
        this.arr_states = null;
        clearTimeout(this.updateTimeout);
    }
    /**
     * 转型
     */
    evolve(big) {
        this.gameMgr = big.gameMgr;
        this.player = big.player;
        this.point = big.point;
        this.mother = big.mother;
        this.father = big.father;
        this.point.big = this;
        this.player.arr_bigs.delete(big);
        this.player.arr_bigs.add(this);
        this.id = big.id;
        this.actionTime = big.actionTime;
        this.terrainAdapt();
        this.onEvolved();
    }
    /** 行动力一次 */
    actionOnce() {
        this.actionTime--;
        this.update();
    }
    /**
     * 刚转型完成，有的转型体要干点什么
     */
    onEvolved() {
        this.update();
    }
    toObj() {
        let obj = {
            "id": this.id,
            "point_id": this.point.id,
            "player_username": this.player.username,
            "father_id": this.father == null ? null : this.father.id,
            "arr_states": Array.from(this.arr_states),
            "actionTime": this.actionTime
        };
        return obj;
    }
    /** 延迟20ms发送,回合开始的时候一直发送不太好 */
    update() {
        clearTimeout(this.updateTimeout);
        this.updateTimeout = setTimeout(() => {
            if (this.gameMgr != null) {
                this.gameMgr.sendBroadcast(GameUtils_1.default.getObj("update_big", { "big": this.toObj() }, true, false), false);
            }
        }, 20);
    }
    /** 大子被攻击
     *  player 凶手，被这个玩家杀死的，子系死亡时特效要给对方看到
     */
    attacked(player) {
        if (this.arr_states.has(BigState.SHIELD)) {
            this.deleteState(BigState.HIDE);
            this.deleteState(BigState.SHIELD);
            return false;
        }
        else {
            this.die(player);
        }
        return true;
    }
    /** 大子死亡
     * murderer 杀人凶手，死在这个人手上的
     * effect_point_id  大子死亡的时候不一定在大子所在位置，比如自己撞对面死了，特效播放在死亡地点
     * is_show_effect   是否显示死亡动画，比如幽灵附身时，不播放
     */
    die(murderer, effect_point_id = null, is_show_effect = true) {
        this.gameMgr.sendBroadcast(GameUtils_1.default.getObj("big_die", {
            "point_id": this.point.id,
            "effect_point_id": effect_point_id,
            "is_show_effect": is_show_effect,
            "murderer": murderer.username
        }));
        this.point.big = null;
        this.player.arr_bigs.delete(this);
        this.is_death = true;
        this.player.arr_bigs.forEach(big => {
            if (big.father == this) {
                big.die(murderer);
            }
        });
    }
    /** 儿子们死亡，转型时执行 */
    sonDie(murderer) {
        this.player.arr_bigs.forEach(big => {
            if (big.father == this) {
                big.die(murderer);
            }
        });
    }
    /** 被恶灵附身 */
    possess(big_恶灵 = null, big_father = null) {
        let old_player = this.player;
        if (big_恶灵 != null) {
            this.father = big_恶灵.father;
            this.mother = big_恶灵.mother;
            this.player.arr_bigs.delete(this);
            this.player = big_恶灵.player;
            this.player.arr_bigs.add(this);
            let father_id = null;
            if (this.father != null) {
                father_id = this.father.id;
            }
            this.gameMgr.sendBroadcast(GameUtils_1.default.getObj("big_possess", { "point_id": this.point.id, "player_username": big_恶灵.player.username, "father_id": father_id }));
        }
        else {
            this.mother = big_father.mother;
            this.player.arr_bigs.delete(this);
            this.player = big_father.player;
            this.player.arr_bigs.add(this);
            this.gameMgr.sendBroadcast(GameUtils_1.default.getObj("big_possess", { "point_id": this.point.id, "player_username": big_father.player.username, "father_id": null }));
        }
        this.update();
        old_player.arr_bigs.forEach(big => {
            if (big.father == this) {
                big.possess(null, this);
            }
        });
        this.turnStart();
    }
    /** 获得状态 */
    addState(state) {
        if (!this.arr_states.has(state)) {
            this.arr_states.add(state);
            this.update();
        }
    }
    /** 删除状态 删除隐身、焦渴 */
    deleteState(state) {
        this.arr_states.delete(state);
        this.update();
    }
    /** 回合开始 */
    turnStart() {
        this.actionTime = 2;
        if (this.arr_states.has(BigState.METHYSIS_BEFORE)) {
            this.addState(BigState.METHYSIS);
            this.deleteState(BigState.METHYSIS_BEFORE);
        }
        this.update();
    }
    /** 回合结束 */
    turnOver() {
        this.actionTime = 0;
        this.deleteState(BigState.ICE);
        this.deleteState(BigState.FEAR);
        this.deleteState(BigState.EXPOSE);
        this.poisonousExplosion();
        this.update();
    }
    /** 地形适应 */
    terrainAdapt() {
        switch (this.point.terrain) {
            case Point_1.Terrain.KONG:
                this.deleteState(BigState.HIDE);
                this.deleteState(BigState.BURDEN);
                break;
            case Point_1.Terrain.SHAN:
                this.die(this.player);
                break;
            case Point_1.Terrain.SHUI:
                if (!this.arr_states.has(BigState.WAVERIDER)) {
                    this.die(this.player);
                }
                break;
            case Point_1.Terrain.LIN:
                this.addState(BigState.HIDE);
                this.deleteState(BigState.BURDEN);
                break;
            case Point_1.Terrain.SHA:
                this.deleteState(BigState.HIDE);
                this.addState(BigState.BURDEN);
                break;
        }
    }
    /** 毒爆 */
    poisonousExplosion() {
        if (!this.arr_states.has(BigState.METHYSIS)) {
            return;
        }
        this.gameMgr.sendBroadcast(GameUtils_1.default.getObj("play_effect", { "effect": "big_毒爆", "point_id": this.point.id, "username": this.player.username }));
        if (this.arr_states.has(BigState.SHIELD)) {
            this.deleteState(BigState.SHIELD);
            this.deleteState(BigState.METHYSIS);
            return;
        }
        let arr = GameUtils_1.default.getArrPointsAround(this.gameMgr, this.point);
        for (let p of arr) {
            let b = p.big;
            if (b != null && b.player == this.player && b.evolution == BigEvolution.未转型) {
                b.addState(BigState.METHYSIS_BEFORE);
            }
        }
        this.die(this.player);
    }
}
exports.Big = Big;
